<?php

//echo br(3); ###########################################################################################

	$TEMP->set_file(array('handle' => DIR_VIEW_DEAN . 'courseSections.html'));
	
	$SID 	= _GET('sid');
	$SLID 	= _GET('slid');
	$HASH 	= _GET('hash');
	
	$semesterLabel  = $DSC->getSemesterLabel($SLID);
	$CID 	    	= $semesterLabel['course_idFK'];
	$semID    		= $semesterLabel['sem_idPK'];
	$semYear   		= $semesterLabel['sem_year'];
	$semSem    		= $semesterLabel['sem_sem'];
	$semDesc    	= $semesterLabel['sem_desc'];
	$semCode    	= $semesterLabel['sem_code'];
	$courseCode 	= $semesterLabel['course_code'];

	if ( isset($_POST['subButton']) ) {
		$if_error	= 0;
		$txtsection	= upperCase(_POST('txtsection'));
		$txtno		= _POST('txtno');
		if ( empty($txtsection) )	{ $if_error++; $_txtsection	= REQ_FIELD; }
		if ( empty($txtno) )	{ $if_error++; $_txtno 			= REQ_FIELD; }
		if ( $if_error == 0 ) {
			if ( !empty($SID) ) {
				$STUD->updateDefaultSections($txtsection, $CID, $SLID, $txtno, $SID);
			}
			else {
				$STUD->insertDefaultSections($txtsection, $CID, $SLID, $txtno);
			}
			redirect('?page=CourseSections&slid='.$SLID.'&hash='.$HASH);
		}
	}
	
	if ( !empty($SID) ) {
		$defaultSections = $STUD->getDefaultSections($CID, $SID, $SLID);
		
		$txtsection	= $defaultSections[0]['section_desc'];
		$txtno		= $defaultSections[0]['no_stud'];
	}

	$getStudentEnrolled = $STUD->getStudentEnrolled($SLID, $CID, $activeYear);
	
	$countStudEnrolled		 = count($getStudentEnrolled);

	$studSecSemAssigned = $STUD->getStudentSectionSemesterAssigned($CID, $SLID, $activeYear);
	
	$countStudSecSemUnassigned	= ($countStudEnrolled - $studSecSemAssigned);
	
	$studUnAssigned = ($countStudSecSemUnassigned > 0) ? $countStudSecSemUnassigned : 0;
	
	$defaultSections = $STUD->getDefaultSections($CID, '', $SLID);

	if ( !empty($defaultSections) ) {
		$TEMP->set_block('handle',  'sections', 'sectionsRow');
		$bgcolor 	= LOOPING_F0F;
		$numCount 	= 1;
		foreach($defaultSections as $defaultSectionsValue) {
			
			$section_id = $defaultSectionsValue['section_idPK'];
			
			$studentSectionSemester = $STUD->getStudentSectionSemester($CID, $section_id, $SLID, $activeYear);
			
			if ($studentSectionSemester > 0 ) {
				if ( $studentSectionSemester >= $defaultSectionsValue['no_stud'] )
					$enrolledStud = $studentSectionSemester . ' Student(s)';
				else {
					$enrolledStud = $studentSectionSemester . ' Student(s) <span class="required-field-blue cursor-pointer" onclick="studSectionAutoGenerate(\'' . $SLID . '\', \'' . $CID . '\', \'' . $section_id . '\');"><img src="' . DIR_IMAGES . 'icon/refresh.png" border="0"/> <u>Auto Generate</u> <span id="loading_generate' . $section_id . '"></span></span>';
				}
			}
			else {
				$enrolledStud = '<div class="required-field-blue cursor-pointer" onclick="studSectionAutoGenerate(\'' . $SLID . '\', \'' . $CID . '\', \'' . $section_id . '\', \'' . $activeYear . '\');"><img src="' . DIR_IMAGES . 'icon/refresh.png" border="0"/> <u>Auto Generate</u> <span id="loading_generate' . $section_id . '"></span></div>';
			}
			
			$edButton = '<div class="icon-button">
							<img src="' . DIR_IMAGES . 'icon/close.png" title="Delete" border="0" onclick="deleteSection(\''. $section_id . '\');"/>
							<img src="' . DIR_IMAGES . 'icon/pencil.png" title="Edit" border="0" onclick="linkNewPage(\'CourseSections&slid='.$SLID.'&sid='.$section_id.'&hash='.$HASH.'\');"/>
							<img src="' . DIR_IMAGES . 'icon/empty.png" title="Empty" border="0" onclick="popupDelete(\'' . $section_id['section_idPK'] . '\', \'300\');"/>
							<div class="delete-button" id="delete_button_' . $section_id['section_idPK'] . '">
								<div class="delete-label">Delete all the Student on this Section?</div>
								<div class="delete-yes-no" onclick="deleteStudSection(\'' . $section_id['section_idPK'] . '\');">Yes</div>
								<div class="delete-yes-no" onclick="popupDeleteHide(\'' . $section_id['section_idPK'] . '\');">No</div>
							</div>
						</div>';
			
			$TEMP->set_var(array('section_deas'  => $defaultSectionsValue['section_desc'],
								 'section_id' 	 => $section_id,
								 'no_stud' 		 => $defaultSectionsValue['no_stud'],
								 'enrolled_stud' => $enrolledStud,
								 'numbering'	 => $numCount,
								 'bgcolor'		 => $bgcolor,
								 'ed_button'	 => $edButton
								));
								
			$TEMP->parse('sectionsRow', 'sections', true);
			$bgcolor = ($bgcolor == LOOPING_F0F) ? LOOPING_FFF : LOOPING_F0F;
			$numCount++;
		}
	}
	else {
		$TEMP->set_var(array('section_deas'  => '',
							 'section_id' 	 => '',
							 'no_stud' 		 => '',
							 'enrolled_stud' => '',
							 'numbering'	 => '',
							 'bgcolor'		 => '',
							 'ed_button'	 => '',
							));
	}
	
	
	$TEMP->set_var(array('dean_sub_menu'	=> $pageLib->deanSubMenu(DIR_IMAGES),
						 'semester_label' 		=> $semDesc . ' (' . $semCode . ')',
						 'total_enrolled'		=> $countStudEnrolled,
						 'assign_enrolled'		=> $studSecSemAssigned,
						 'unassign_enrolled'	=> $studUnAssigned,
						 'section_generate'		=> $sectionGenerate,
						 'course_id'			=> $CID,
						 'w_txtsection' 		=> $_txtsection,
						 'w_txtno' 				=> $_txtno,
						 'txtsection' 			=> $txtsection,
						 'txtno' 				=> $txtno,
						 'slid'					=> $SLID,
						 'hash'					=> $HASH,
						 'reg_enrolled'			=> $countRegStudEnrolled,
						 'irreg_enrolled'		=> $countIrregStudEnrolled,
						));
?>